package com.zyt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyt.dao.TeaMapper;
import com.zyt.dao.UserMapper;
import com.zyt.pojo.Tea;
import com.zyt.pojo.User;
import com.zyt.service.TeaService;
import com.zyt.util.MyBitisUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TeaServiceImpl implements TeaService {
    /*@Override
    public List<Tea> selectAll() {
        SqlSession sqlSession= null;
        List<Tea> teaList=null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
            teaList= teaMapper.selectAll();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
        return teaList;
    }*/

    @Override
    public void addTea(Tea tea) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
            teaMapper.addTea(tea);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
    }

    @Override
    public void deleteById(Integer id) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
            teaMapper.deleteById(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
    }

    @Override
    public void updateById(Tea tea) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
            teaMapper.updateById(tea);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
    }

    @Override
    public Tea selectById(Integer id) {
        SqlSession sqlSession= null;
        Tea tea=null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
            tea= teaMapper.selectById(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
        return tea;
    }

    @Override
    public void deleteByIdAndCate(Integer id) {
        SqlSession sqlSession= null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
            teaMapper.deleteByIdAndCate(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
    }

    @Override
    public List<Tea> selectByCatID(Integer id) {
        SqlSession sqlSession= null;
        List<Tea> teaList=null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
            teaList= teaMapper.selectByCatID(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
        return teaList;
    }

    @Override
    public PageInfo<Tea> servletAll(int curPage, int pageSize) {
        SqlSession sqlSession= null;

        PageInfo<Tea> pageInfo=null;
        try {
            sqlSession= MyBitisUtil.getSession();
            TeaMapper teaMapper =sqlSession.getMapper(TeaMapper.class);
//            告知mybatis要分页了，并传入开始的数据和每页展示几条数据
            PageHelper.startPage(curPage, pageSize);
//            调用方法把本页的所有数据都放到集合里
            List<Tea>  studentList = teaMapper.selectAll();
//            把本页数据都封装到pageinfo对象中
            pageInfo=new PageInfo<>(studentList);
            System.out.println(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBitisUtil.closeSession(sqlSession);
        }
        return pageInfo;
    }
}
